/**
  ******************************************************************************
  * Copyright(c) 2018-2023 WUHAN LEADOR All rights reserved. 
  * - Filename    
  * - Author  Vincent
  * - Version V1.0.0
  * - Date    2019/4/16
  * - Brief   
  * - FunctionList:
  ******************************************************************************
  * History:
  *   
  *
  * 
  ******************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include "socket_server_util.h"
#include "dbg.h"

/* prama define discrible -----------------------------------------------------*/

/* strcuct  discrible --------------------------------------------------------*/

/* prama  discrible ---------------------------------------------------------*/

/* private fucntion  declaration --------------------------------------------*/

/* private fucntion  discrible -----------------------------------------------*/
/*------------------------------------ ----------------------------------------
- @Name:  
- @Discrible:   
- @Param:  
- @Return:   
- @Others: 
------------------------------------ ----------------------------------------
- @Recode    date       version	        author             modify 
-          ------------------------------------------------------------------
-          20190416      V1.0          VINCENT            Create
- 
------------------------------------ ----------------------------------------*/
int open_listenfd(int port)
{
    int listenfd, optval = 1;
    struct sockaddr_in serveraddr;
    
    /* Create a socket descriptor */
    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        return -1;

    /* Eliminates "Address already in use" error from bind. */
    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
                   (const void *)&optval, sizeof(int)) < 0)
        return -1;

    /* Listenfd will be an endpoint for all requests to port
       on any IP address for this host */
    bzero((char *)&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons((unsigned short)port);
    if (bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
        return -1;

    /* Make it a listening socket ready to accept connection requests */
    if (listen(listenfd, LISTENQ) < 0)
        return -1;  

    return listenfd;
}

/******************************************************************************
* @Function:  
* @Discrible:   
* @Param:  
* @Return:   
* @Others: 
******************************************************************************
* @Recode    date       version	        author             modify 
*          ------------------------------------------------------------------
*          20190416      V1.0          VINCENT            Create
* 
*******************************************************************************/
int make_socket_non_blocking(int fd)
{
    int flags, s;
    flags = fcntl(fd, F_GETFL, 0);
    if (flags == -1)
    {
        log_err("fcntl");
        return -1;
    }

    flags |= O_NONBLOCK;
    s = fcntl(fd, F_SETFL, flags);
    if (s == -1)
    {
        log_err("fcntl");
        return -1;
    }
    return 0;
}


/******************************************************************************
* @Function:  
* @Discrible:   
* @Param:  
* @Return:   
* @Others: 
******************************************************************************
* @Recode    date       version	        author             modify 
*          ------------------------------------------------------------------
*          20190416      V1.0          VINCENT            Create
* 
*******************************************************************************/

/************************ (.cpp) END OF FILE ************************************/